package com.huan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huan.VO.CategoryVo;
import com.huan.VO.HotCategoryVo;
import com.huan.VO.LinkVo;
import com.huan.VO.PageVo;
import com.huan.constant.SystemConstants;
import com.huan.domain.ResponseResult;
import com.huan.domain.entity.Category;
import com.huan.domain.entity.Link;
import com.huan.mapper.CategoryMapper;
import com.huan.service.ArticleService;
import com.huan.utils.BeanCopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huan.service.CategoryService;

import java.util.List;

/**
 * 分类表(Category)表服务实现类
 *
 * @author makejava
 * @since 2023-06-06 17:04:54
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    ArticleService articleService;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public ResponseResult getCategoryList() {
//        查询status为0的文章
//        需要使用已经发布过文章的id
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        List<Category> list = list(wrapper);
        List<HotCategoryVo> hotCategoryVos = BeanCopyUtil.resultAllVo(list, HotCategoryVo.class);
        ResponseResult result = ResponseResult.okResult(hotCategoryVos);
        return result;
    }

    @Override
    public ResponseResult getAllCategory() {
        List<Category> list = list(null);
        List<CategoryVo> categoryVos = BeanCopyUtil.resultAllVo(list, CategoryVo.class);
        return ResponseResult.okResult(categoryVos);
    }

    @Override
    public ResponseResult<PageVo> getAllList(Integer pageNum, Integer pageSize, String name, String status) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getName,name);
        wrapper.eq(Category::getStatus,status);
        Page<Category> page = new Page<>(pageNum, pageSize);
        Page<Category> categoryPage = page(page, wrapper);
        List<Category> categories = categoryPage.getRecords();
        List<CategoryVo> categoryVos = BeanCopyUtil.resultAllVo(categories, CategoryVo.class);
        PageVo pageVo = new PageVo(categoryVos, page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult add(Category category) {
        save(category);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getLink(Long id) {
        Category category = getById(id);
        CategoryVo categoryVo = BeanCopyUtil.resultVO(category, CategoryVo.class);

        return ResponseResult.okResult(categoryVo);
    }

    @Override
    public ResponseResult uplink(Category category) {
        updateById(category);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteLink(Long id) {
        categoryMapper.deleteById(id);
        return ResponseResult.okResult();
    }


}

